<?php
namespace app\common\service;

use plug\SHttp;

class Yunpian
{
	private $key = '89384d7f65524a1dfe37e444aeec029a';
	private $theHeaders = [
		"Accept:text/plain;charset=utf-8",
		"Content-Type:application/x-www-form-urlencoded",
		"charset=utf-8",
	];

	/**
	 * @title 发送验证码
	 * @param $phones
	 * @param $code
	 * @param $name
	 * @return array|string
	 */
	public function Sendvcode($phones, $code,$name='蜂族到家')
	{
		$content = '【'.$name.'】您的验证码是' . $code;
		$url = "https://sms.yunpian.com/v2/sms/single_send.json";
		$data = http_build_query(array(
			'apikey' => $this->key,
			'mobile' => $phones,
			'text' => $content
		));

		$rs = SHttp::http_curl($url, $data, $this->theHeaders, 'post');
		return $rs;
	}

	/**
	 * @title 通知有用户购买商品成功
	 * @param $phones
	 * @param $orderno
	 * @param $order_time
	 * @param $pay_time
	 * @param $name
	 * @return mixed
	 */
	public function SendNoticeOrder($phones,$orderno,$order_time,$pay_time,$name='蜂族到家'){
		$url = "https://sms.yunpian.com/v2/sms/single_send.json";
		$content = "【{$name}】您有订单来了！订单号：{$orderno}，下单时间：{$order_time}，支付时间：{$pay_time}，请登录后台查看详细信息！";
		$data = http_build_query(array(
			'apikey' => $this->key,
			'mobile' => $phones,
			'text' => $content
		));

		$rs = SHttp::http_curl($url, $data, $this->theHeaders, 'post');
		return json_decode($rs['str'],true);
	}

	/**
	 * @title 通知有用户取消商品
	 * @param $phones
	 * @param $name
	 * @param $type
	 * @return array|string
	 */
	public function SendNoticeCancelOrder($phones,$orderno,$time,$name='蜂族到家'){
		$url = "https://sms.yunpian.com/v2/sms/single_send.json";
		$content = "【{$name}】订单号：{$orderno}，于{$time}取消下单，请登录后台查看详细信息！";
		$data = http_build_query(array(
			'apikey' => $this->key,
			'mobile' => $phones,
			'text' => $content
		));

		$rs = SHttp::http_curl($url, $data, $this->theHeaders, 'post');
		return json_decode($rs['str'],true);
	}

	/**
	 * @title 通知有用户购买服务成功
	 * @param $phones
	 * @param $orderno
	 * @param $user_name
	 * @param $product_name
	 * @param $time
	 * @param $name
	 * @return mixed
	 */
	public function SendNoticeservice($phones,$orderno,$user_name,$product_name,$time,$name='蜂族到家'){
		$url = "https://sms.yunpian.com/v2/sms/single_send.json";
		$content = "【{$name}】订单号：{$orderno} ，用户名{$user_name}预约了{$product_name}服务，预约时间：{$time}, 请登录后台查看详细信息！";
//		echo $content;die;
		$data = http_build_query(array(
			'apikey' => $this->key,
			'mobile' => $phones,
			'text' => $content
		));

		$rs = SHttp::http_curl($url, $data, $this->theHeaders, 'post');
		return json_decode($rs['str'],true);
	}

	/**
	 * @title 通知有用户取消服务
	 * @param $phones
	 * @param $name
	 * @param $type
	 * @return array|string
	 */
	public function SendNoticeCancelservice($phones,$nick_name,$time,$product_name,$name='蜂族到家'){
		$Y=date("Y",$time);
		$m=date("m",$time);
		$d=date("d",$time);
		$H=date("H",$time);
		$i=date("i",$time);
		$time = $Y.'年'.$m.'月'.$d.'日'.$H.'时'.$i.'分';
		$url = "https://sms.yunpian.com/v2/sms/single_send.json";
		$content = "【{$name}】{$nick_name}预约{$time}的{$product_name}服务已取消，请登录后台查看详细信息! ";
		$data = http_build_query(array(
			'apikey' => $this->key,
			'mobile' => $phones,
			'text' => $content
		));

		$rs = SHttp::http_curl($url, $data, $this->theHeaders, 'post');
		return json_decode($rs['str'],true);
	}
}
